<template>
  <div ref="moreBar"></div>
</template>

<script>
import resize from '@/mixins/resize'
import * as echarts from 'echarts'
import { findAllCourse } from '@/api/config'
export default {
  name: 'moreBar',
  mixins: [resize],
  data () {
    return {
      course: [],
      courseNum: [],
      signNum: []
    }
  },
  async mounted () {
    await this.getCourse()
    this.$nextTick(() => {
      this.initEchart()
    })
  },
  methods: {
    async getCourse () {
      let params = {
        operate: 'manage',
        pageSize: 10000,
        pageNum: 1
      }
      let res = await findAllCourse(params)
      if (res.code === 200) {
        let courses = res.data.items.filter(t => t.status === 'COMPLETED' || t.status === 'RECRUITING')
        this.course = courses.map(t => { return t.name })
        this.courseNum = courses.map(t => { return t.recruitNum })
        this.signNum = courses.map(t => { return t.campaignUserInfos ? t.campaignUserInfos.length : 0 })
      }
    },
    initEchart () {
      this.myChart = echarts.init(this.$refs.moreBar)
      this.myChart.setOption(
        {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            },
            padding: [5, 10]
          },
          grid: {
            left: 50,
            right: 20,
            bottom: 40,
            top: 30
          },
          legend: {
            show: true,
            itemWidth: 16,
            itemHeight: 12,
            textStyle: {
              color: '#B4B4B4'
            }
          },
          xAxis: [
            {
              type: 'category',
              data: this.course,
              axisLine: {
                lineStyle: {
                  color: '#B4B4B4'
                }
              },
              axisLabel: {
                color: '#B4B4B4'
              },
              axisTick: {
                show: false
              }
            }
          ],
          yAxis: [
            {
              type: 'value',
              axisTick: {
                show: false
              },
              axisLine: {
                lineStyle: {
                  color: '#B4B4B4'
                }
              },
              axisLabel: {
                color: '#B4B4B4'
              },
              splitLine: {
                show: false
              }
            }
          ],
          series: [
            {
              name: '招募人数',
              type: 'bar',
              data: this.courseNum,
              barWidth: '8',
              barGap: '20%',
              itemStyle: {
                normal: {
                  barBorderRadius: 10,
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: '#5171fd'
                    },
                    {
                      offset: 1,
                      color: '#c97afd'
                    }
                  ])
                }
              }
            },
            {
              name: '报名人数',
              type: 'bar',
              data: this.signNum,
              barWidth: '8',
              barGap: '20%',
              itemStyle: {
                normal: {
                  barBorderRadius: 10,
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: '#3dadf6'
                    },
                    {
                      offset: 1,
                      color: '#737bfc'
                    }
                  ])
                }
              }
            }
          ]
        },
        true
      )
    }
  }
}
</script>
